const ald = require('./utils/ald-stat.js');

//app.js
const Promise = require('utils/bluebird.min.js');
const md5 = require('utils/md5.js');
import io from "./utils/index.js";
import Base64 from "./utils/base64.js";


App({

  onLaunch: function (ops) {
    if (ops.scene == 1044) {
      console.log(ops.shareTicket);
      wx.getShareInfo({
        shareTicket: ops.shareTicket,
        success(res) {
          console.log("Launch获取群加密信息")
          console.log(res);
          wx.setStorageSync('ShareInfo', res);
        },
        fail() {
          console.log('失败');
        },
        complete() { }
      });
    }
    this.globalData.socket.on('connect', () => {
      console.log('wss 已链接');
      this.globalData.isconnect = true;
    });
    this.globalData.socket.on('disconnect', (reason) => {
      console.log('wss 已断开');
    });
    //调用API从本地缓存中获取数据
    if (wx.getUpdateManager) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate)
      })
      updateManager.onUpdateReady(function () {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        })
      })
      updateManager.onUpdateFailed(function () {
        // 新的版本下载失败
      })
    
    }
   
    
  },
  onShow: function (ops){
    if (ops.scene == 1044) {
      console.log(ops.shareTicket);
      wx.getShareInfo({
        shareTicket: ops.shareTicket,
        success(res) {
          console.log("show获取群加密信息")
          console.log(res);
          wx.setStorageSync('ShareInfo', res);
        },
        fail() {
          console.log('失败');
        },
        complete() { }
      });
    }
  },
  

  globalData: {
    userInfo: null,
    api: 'https://ccy.uurnn.com/api4.php?source=1&s=',
    //api: 'https://ccydq.yoco365.com/api4.php?source=1&s=',
    //api:'http://10.0.0.90/wxccyss/api4.php?source=1&s=',
    headers: {},
    socket: io('wss://wss.9istudy.com?type=caichengyu', { autoConnect: false }),
    md5: md5,
    isconnect: false,
    Base64: Base64,
  },
  getSession:function(){
    wx.login({   // 获取code
      success: res => {
        if (res.code) {
          wx.request({ // 获取openid
            url: this.globalData.api + '/wx/openid.html',
            method: 'GET',
            data: {
              code: res.code
            },
            success: res => {
              if (res.data.code == 0) {
                console.log('res===', res.data)
              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg);
          reject('error');
        }
      }
    })
  },
  fetchError: function (code,msg) {
    if(code==-1){
      wx.showToast({
        title: msg,
        icon:'none',
        success:()=>{
          wx.redirectTo({
            url: '/pages/index/index',
          })
        }
      })
    }

  },
  getShareIMG:function(){
    var i=2;
    var path = '../../images/share' + Math.round(Math.random() * i)+'.png';
    return path;
  },
  getShareHBIMG: function () {
    var i = 4;
    var path = '../../images/sharehb' + Math.round(Math.random() * i) + '.png';
    return path;
  },
  getShareTZIMG: function () {
    var i = 2;
    var path = '../../images/share' + Math.round(Math.random() * i) + '.png';
    return path;
  },
  getShareFBIMG: function () {
    var i = 2;
    var path = '../../images/sharefb' + Math.round(Math.random() * i) + '.png';
    return path;
  },
  getShareDefault:function(){
    var i=3;
    var rd = Math.round(Math.random() * i);
    var ShareObj={};
    var sharelist = ['据说智商160以上才能全部答对，我答对了10题，你呢？', '震惊！高中女孩竟然靠这游戏写出满分作文...', '这游戏全靠脑子，聪明就是可以为所欲为！','玩这个游戏的个个是学霸，说话又好听，我超喜欢的说！'];
    ShareObj.sharetxt = sharelist[rd];
    ShareObj.imgurl = '../../images/sharedefault'+rd+'.png';

    return ShareObj;
  },
})